<template>
  <div>
    <el-form :model="form" label-width="auto" inline>
      <el-form-item label="收货仓库:">
        <el-select v-model="form.warehouseId">
          <el-option label="全部" :value="0"></el-option>
          <el-option :label="item.value" :value="item.key"
            v-for="item in store.productGearPurchaseStoragesearchWarehouseDicTable" clearable :key="item.key"
            @click="getKuWei(item.warehouseLocationDicList)" />
        </el-select>
      </el-form-item>
      <el-form-item label="库位号:">
        <el-select v-model="form.warehouseLocationId">
          <el-option label="全部" :value="0" />
          <el-option :label="item.value" :value="item.key" v-for="item in KW" clearable :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="拿货号">
        <template #default="scope">{{ scope.row.purchaseCode }}</template>
      </el-table-column>
      <el-table-column label="拿货时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="仓库信息" width="180">
        <template #default="scope">{{ scope.row.warehouseName }}</template>
      </el-table-column>
      <el-table-column label="货物区域" width="180">
        <template #default="scope"><span>{{
          scope.row.storageWarehouseArea ? JSON.parse(scope.row.storageWarehouseArea).warehouseAreaData[0].name : ''
        }}</span></template>
      </el-table-column>
      <el-table-column label="库位信息" width="180">
        <template #default="scope"><span style="color: red;">{{
          scope.row.storageWarehouseLocation ?
            JSON.parse(scope.row.storageWarehouseLocation).warehouseLocationData[0].name : ''
        }}</span></template>
      </el-table-column>
      <el-table-column label="商品信息" width="180">
        <template #default="scope"><span style="color: blue;">{{ scope.row.stockNum }}</span></template>
      </el-table-column>
      <el-table-column label="入库数量">
        <template #default="scope">{{ scope.row.storageNum }}</template>
      </el-table-column>
      <el-table-column label="拿货价">
        <template #default="scope">{{ scope.row.purchasePrice }} <span style="color: blue;" class="headclass"
            @click="openEvery(scope.row.productId)">更多</span> </template>
      </el-table-column>
      <el-table-column label="出库价">
        <template #default="scope">{{ scope.row.outPrice }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="printProductPurchaseStorageBtn(scope.row)">
            打印入库单
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="returnON(scope.row)">
            取消入库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <div>
    <el-dialog v-model="EverydialogVisible" width="750">
      <div v-if="purchasePriceOr">
        <el-form :model="purchasePriceform" inline>
          <el-form-item label="出库价">
            <el-input v-model="purchasePriceform.outPrice" />
          </el-form-item>
          <el-form-item label="拿货价">
            <el-input v-model="purchasePriceform.purchasePrice" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setpurchasePriceOnSubmit">确定</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="productGearPurchasepriceListTAblk" border style="width: 100%">
        <el-table-column prop="outPrice" label="出库价" width="120" />
        <el-table-column prop="purchasePrice" label="拿货价" width="120" />
        <el-table-column prop="purchaseTime" label="记录时间" width="220" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" size="small" @click.prevent="setpurchasePriceOr(scope.row)">
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount, onBeforeUnmount } from 'vue'
import { useAlertsStore } from '@/store/index'
//
import { productGearPurchaseStorageList, productGearPurchaseStoragePrint, warehousetUserDefault, cancelProductGearPurchaseStorageStatus, productGearPurchasepriceList, productGearPurchaseUpdate } from '@/api'
import { ElMessage } from 'element-plus'
const store = useAlertsStore()
const form = ref({
  pageSize: 10,
  pageNum: 1,
  storageStatus: 1,
  warehouseLocationId: 0
})
onBeforeMount(() => {
  doConnect()
  store.productGearPurchaseStoragesearchWarehouseDicBtn()
  userDefaultUserDefaultBtn()
})
const onSubmit = () => {
  getList()
}
const tableData = ref([])
const getList = () => {
  productGearPurchaseStorageList(form.value).then(res => {
    const { data: { data } } = res
    tableData.value = data.list
    total.value = data.total
  })
}
const userDefaultUserDefaultBtn = async () => {
  const a = await warehousetUserDefault()
  const { data: { data } } = a
  console.log('datas')
  console.log(data)
  console.log(store.productGearPurchaseStoragesearchWarehouseDicTable)
  store.productGearPurchaseStoragesearchWarehouseDicTable.forEach(e => {
    if (e.value === data) {
      form.value.warehouseId = e.key
    }
  })
}
// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}
const KW = ref([])
const getKuWei = (row) => {
  KW.value = row
}
const valA4 = ref([])
const taketable = ref({ areaShop: '', storagelocation: '', num: '', batchNum: '', pdaCode: '', skuInfos: '' })
const printProductPurchaseStorageBtn = (row) => {
  valA4.value = []
  productGearPurchaseStoragePrint(row.id).then(res => {
    ElMessage.success('操作成功')
    const { data: { data } } = res
    const a = JSON.parse(data.storageWarehouseLocation).warehouseLocationData
    a.forEach(element => {
      taketable.value.areaShop = data.warehouseName
      taketable.value.storagelocation = element.name
      taketable.value.num = element.needSave
      taketable.value.skuInfos = data.skuInfo
      taketable.value.batchNum = data.inboundBatchesCode
    })
    valA4.value.push(taketable.value)
    getList()
    console.log('tableData.value')
    console.log(valA4.value)
    if (localStorage.getItem('PrinterSetListRK')) {
      const lists = JSON.parse(localStorage.getItem('PrinterSetListRK'))
      console.log(lists[0].PrinterList)

      const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '210', height: '297', padding: '2', fontSize: '8', left: '2', padcode: data.pdaCode, skuInfo: valA4.value, barcodeHeight: '33' }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/a4.xml' }] }] } }
      // const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { waybillCode: data.pdaCode, skuInfo: valA4.value }, templateURL: `http://159.75.104.145:18889/config/sysPrintTemplateSet/getPrintTemplateByOrderId?id=${3}&userName=${localStorage.getItem('system_name')}` }] }] } }
      store.socket.send(JSON.stringify(val))
    } else {
      ElMessage.success('先设置打印机')
    }
  })
}
// 销毁
onBeforeUnmount(() => {
  console.log('销毁')
  store.destroyed()
})
const doConnect = () => {
  console.log('d打开了不')
  store.socket = new WebSocket('ws://localhost:13528')
  // 打开Socket
  store.socket.onopen = function (event) {
    // 监听消息
    store.socket.onmessage = function (event) {
      // localStorage.setItem('printerproperties', event.data)
    }
    // 监听Socket的关闭
    store.socket.onclose = function (event) {
      // console.log('Client notified socket has closed', event)
    }
  }
}
const returnON = (row) => {
  cancelProductGearPurchaseStorageStatus(row.id).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}

const EverydialogVisible = ref(false)
const productGearPurchasepriceListTAblk = ref([])
const idsOld = ref('')
const openEvery = (id) => {
  if (id) {
    idsOld.value = id
    productGearPurchasepriceList(id).then(res => {
      const { data: { data } } = res
      productGearPurchasepriceListTAblk.value = data
      EverydialogVisible.value = true
    })
  } else {
    productGearPurchasepriceList(idsOld.value).then(res => {
      const { data: { data } } = res
      productGearPurchasepriceListTAblk.value = data
      EverydialogVisible.value = true
    })
  }
}
const purchasePriceform = ref({ productGearPurchaseIds: [] })
const purchasePriceOr = ref(false)
const setpurchasePriceOr = (row) => {
  purchasePriceform.value.productGearPurchaseIds = []
  purchasePriceform.value.productGearPurchaseIds.push(row.productGearPurchaseId)
  purchasePriceOr.value = true
}
const setpurchasePriceOnSubmit = () => {
  productGearPurchaseUpdate(purchasePriceform.value).then(res => {
    ElMessage.success('修改成功')
    purchasePriceform.value = { setpurchasePriceOnSubmit: '' }
    openEvery()
    purchasePriceOr.value = false
  })
}
</script>

<style lang='less' scoped></style>
